package com.ruitu.mall_98.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.BaseActivity;
import com.ruitu.mall_98.MainActivity;
import com.ruitu.mall_98.R;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends BaseActivity {
    private ViewPager viewPager;
    private TextView tv_go_in;
    private LinearLayout llPoint;

    private int[] imageView = {R.drawable.guide_1, R.drawable.guide_2, R.drawable.guide_3};
    private List<View> views = new ArrayList<>();

    private GuideAdapter adapter;
    private boolean misScrolled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        Arad.preferences.putString("guide", "guide").flush();
        viewPager = findViewById(R.id.viewPager);
        tv_go_in = findViewById(R.id.tv_go_in);
        llPoint = findViewById(R.id.llPoint);
        setListener();
        initData();
//        addPoint();
    }

    private void initData() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        for (int i = 0; i < imageView.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(params);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(imageView[i]);
            views.add(iv);
        }
        adapter = new GuideAdapter(views);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == views.size() - 1) {
                    tv_go_in.setVisibility(View.VISIBLE);
                } else {
                    tv_go_in.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        misScrolled = false;
                        break;
                    case ViewPager.SCROLL_STATE_SETTLING:
                        misScrolled = true;
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        if (viewPager.getCurrentItem() == viewPager.getAdapter().getCount() - 1 && !misScrolled) {
                            startActivity(MainActivity.class);
                            finish();
                        }
                        break;
                }
            }
        });
    }

    private void setListener() {
        tv_go_in.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(MainActivity.class);
                finish();
            }
        });
    }

    class GuideAdapter extends PagerAdapter {
        private List<View> list;

        public GuideAdapter(List<View> mList) {
            this.list = mList;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }
    }

    //    private void addPoint() {//添加小圆点
//        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(20, 20);
//        mParams.setMargins(10, 0, 10, 0);//设置小圆点左右之间的间隔
////        dotViews = new ImageView[imageView.length];
//        //判断小圆点的数量，从0开始，0表示第一个
//        for (int i = 0; i < views.size(); i++) {
//            ImageView imageView = new ImageView(this);
//            imageView.setLayoutParams(mParams);
//
//            imageView.setImageResource(R.drawable.guide_selector);
//            if (i == 0) {
//                imageView.setSelected(true);//默认启动时，选中第一个小圆点
//            } else {
//                imageView.setSelected(false);
//            }
////            dotViews[i] = imageView;//得到每个小圆点的引用，用于滑动页面时，（onPageSelected方法中）更改它们的状态。
//            llPoint.addView(imageView);//添加到布局里面显示
//        }
//    }
}
